<template>
  <!---搜索表单-->
  <div class="search-div">
    <el-form label-width="70px" size="small">
      <el-row>
        <el-col :span="12">
          <el-form-item label="关键字">
            <el-input
              v-model="queryDto.activityName"
              style="width: 100%"
              placeholder="活动名称"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="活动时间">
            <el-date-picker
              v-model="activityTimes"
              type="datetimerange"
              range-separator="To"
              start-placeholder="开始时间"
              end-placeholder="结束时间"
              format="YYYY-MM-DD HH:mm:ss"
              value-format="YYYY-MM-DD HH:mm:ss"
            />
            <!-- <el-date-picker
              v-model="createTimes"
              type="daterange"
              range-separator="To"
              start-placeholder="开始时间"
              end-placeholder="结束时间"
              format="YYYY-MM-DD"
              value-format="YYYY-MM-DD"
            /> -->
          </el-form-item>
        </el-col>
      </el-row>
      <el-row style="display:flex">
        <el-button type="primary" size="small" @click="searchActivity">
          搜索
        </el-button>
        <el-button size="small" @click="resetData">重置</el-button>
      </el-row>
    </el-form>
  </div>

  <!--添加按钮-->
  <div class="tools-div">
    <el-button type="success" size="small" @click="addShow">添 加</el-button>
  </div>

  <el-dialog v-model="dialogVisible" title="添加或修改" width="40%">
    <el-form label-width="120px">
      <el-form-item label="活动名称">
        <el-input v-model="activityRule.activityName" />
      </el-form-item>
      <el-form-item label="活动类型">
        <el-input v-model="activityRule.activityType" />
      </el-form-item>
      <el-form-item label="活动描述">
        <el-input
          v-model="activityRule.activityDesc"
          maxlength="100"
          :autosize="{ minRows: 5, maxRows: 8 }"
          style="width: 1000px"
          placeholder="请输入活动描述..."
          show-word-limit
          type="textarea"
        />
      </el-form-item>
      <el-form-item label="活动时间">
        <el-date-picker
          v-model="activityTimes"
          type="datetimerange"
          range-separator="To"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
          format="YYYY-MM-DD HH:mm:ss"
          value-format="YYYY-MM-DD HH:mm:ss"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submit">提交</el-button>
        <el-button @click="dialogVisible = false">取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>

  <!---数据表格-->
  <el-table :data="list" style="width: 100%">
    <el-table-column prop="activityName" label="活动名称" />
    <el-table-column prop="activityType" label="活动类型" />
    <el-table-column prop="activityDesc" label="活动描述" />
    <el-table-column prop="startTime" label="活动开始时间" />
    <el-table-column prop="endTime" label="活动结束时间" />
    <el-table-column prop="createTime" label="创建活动的时间" />
    <el-table-column label="操作" align="center" width="280" #default="scope">
      <el-button type="primary" size="small" @click="editSysUser(scope.row)">
        修改
      </el-button>
      <el-button type="danger" size="small" @click="rule(scope.row)">
        规则
      </el-button>
      <el-button type="danger" size="small" @click="deleteById(scope.row)">
        删除
      </el-button>
    </el-table-column>
  </el-table>

  <el-pagination
    v-model:current-page="queryDto.pageNum"
    v-model:page-size="queryDto.pageSize"
    :page-sizes="[10, 20, 50, 100]"
    @size-change="fetchData"
    @current-change="fetchData"
    layout="total, sizes, prev, pager, next"
    :total="total"
  />
</template>

<script setup>
import { ref, onMounted } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";

import { GetActivityInfo, SaveActivityInfo, FindActivityRuleList } from "@/api/activity/activity";

//规则
const rule = async (row) => {
  const { data } = await FindActivityRuleList(row.id);
  console.log(data);
};

// 表格数据模型
const list = ref([]);

// 分页条数据模型
const total = ref(0);

// 定义搜索表单数据模型
const queryDto = ref({
  pageNum: 1,
  pageSize: 10,
  activityName: "",
  activityType: "",
  startTime: "",
  endTime: ""
});

const activityTimes = ref([]);

// onMounted钩子函数
onMounted(() => {
  fetchData();
});

// 搜素按钮点击事件处理函数
const searchActivity = () => {
  queryDto.value.startTime = activityTimes.value[0];
  queryDto.value.endTime = activityTimes.value[1];
  fetchData();
};

// 重置按钮点击事件处理函数
const resetData = () => {
  queryDto.value = {
    pageNum: 1,
    pageSize: 10,
    activityName: "",
    activityType: "",
    startTime: "",
    endTime: ""
  };
  activityTimes.value = [];
  fetchData();
};

// 定义分页查询方法
const fetchData = async () => {
  const { data } = await GetActivityInfo(queryDto.value);
  console.log(data);

  list.value = data.records;
  total.value = data.total;
};

// 添加表单对话框显示隐藏控制变量
const dialogVisible = ref(false);
const addShow = () => {
  activityRule.value = {};
  activityTimes.value = "";
  dialogVisible.value = true;
};

// 定义提交表单数据模型
const defaultForm = {
  activityName: "",
  activityType: "",
  activityDesc: "",
  startTime: "",
  endTime: ""
};
const activityRule = ref(defaultForm);

// 提交按钮事件处理函数
const submit = async () => {
  activityRule.value.startTime = activityTimes.value[0];
  activityRule.value.endTime = activityTimes.value[1];
  if (!activityRule.value.id) {
    console.log(activityRule.value);
    const { code, message, data } = await SaveActivityInfo(activityRule.value);
    if (code === 200) {
      dialogVisible.value = false;
      ElMessage.success("操作成功");
      fetchData();
    } else {
      ElMessage.error(message);
    }
  }
};
</script>

<style scoped>
.search-div {
  margin-bottom: 10px;
  padding: 10px;
  border: 1px solid #ebeef5;
  border-radius: 3px;
  background-color: #fff;
}

.tools-div {
  margin: 10px 0;
  padding: 10px;
  border: 1px solid #ebeef5;
  border-radius: 3px;
  background-color: #fff;
}

.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}

.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>
